/**
 * @author 王琪 <wangqi@huizhaofang.com>
 * @since 2020-03-08
 */
package com.diploma.project.aiqiyi.controller;

import com.diploma.project.aiqiyi.CommonResponse;
import com.diploma.project.aiqiyi.common.annotation.AuthControl;
import com.diploma.project.aiqiyi.model.User;
import com.diploma.project.aiqiyi.service.UserService;
import com.diploma.project.aiqiyi.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

//    @GetMapping("users/") // http://locahost:8090/user/?nickname=xxxx
//    public CommonResponse<List<User>> getAllUsers(String nickname) {
//        return CommonResponse.success(userService.queryAllUsers());
//    }

    @PostMapping("user/register/") // Postman
    public CommonResponse<User> reginster(Long mobile, String password, String nickname) {
        if (mobile == null) {
            CommonResponse.fail(403, "密码不能为空", null);
        }
        if (StringUtil.isBlank(password)) {
            CommonResponse.fail(403, "密码不能为空", null);
        }

        User user = userService.createUser(mobile, password, nickname);
        return CommonResponse.success(user);
    }

    @GetMapping("user/login/") // http://locahost:8090/user/login/?mobile=2xccxcx&passwor=dsdsd
    public CommonResponse<User> login(Long mobile, String password) {
        User user = userService.login(mobile, password);
        return CommonResponse.success(user);
    }

    @AuthControl
    @GetMapping("user/")
    public CommonResponse<User> getUserInfo() {
        return CommonResponse.success(userService.queryUserInfo());
    }

    @AuthControl
    @PostMapping("user/updatepassword/")
    public CommonResponse<User> updatepassword(String newPassword) {
        userService.updatepassword(newPassword);
        return CommonResponse.ok();
    }

    @AuthControl
    @GetMapping("user/exitlogin/")
    public CommonResponse<User> exitLogin(){
        userService.exitLogin();
        return CommonResponse.ok();
    }



}
